home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8487 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.3 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Memory. How is it organised?
  5. Date: 4 Mar 1996 09:34:32 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hf9j8INNko4@keats.ugrad.cs.ubc.ca>
  8. References: <4hf5gs$1f7@news.mistral.co.uk>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4hf5gs$1f7@news.mistral.co.uk>,
  12. Mike Barnard <mikebarnard@mistral.co.uk> wrote:
  13.  >Hi all.
  14.  >
  15.  >I want to find out as much as possible about the stack and
  16.  >the heap. I know some of this is machine specific, but not
  17.  >all of it.
  18.  
  19. ALL of it is machine specific, make no mistake.
  20.  
  21.  >Suppose I did this?
  22.  >
  23.  >struct tag
  24.  >    {
  25.  >    // Variables taking 100 bytes, for an example
  26.  >    };
  27.  >
  28.  >struct tag arrayname[50];
  29.  >
  30.  >What happens? Is 5000 bytes of memory reserved for the
  31.  >contents of the array? Where? Stack, heap or somewhere else?
  32.  
  33. That depends on where you declared the storage. If it is declared inside a
  34. function body, then the storage is part of the activation record of the
  35. function. Such variables are called ``automatic'' in C. Note my use of the the
  36. compound word ``activation record'' to avoid saying ``stack''. 
  37.  
  38. If you declare it outside of a function, or if you label it ``static'' inside a
  39. function, it is conceptually found in a global memory area.
  40.  
  41.  >Or does the compiler just make a "note" that it will be
  42.  >needed then allocate some memory when the array(s) are
  43.  >initialised. Again, where? (What is the definition of stack
  44.  >or heap?)
  45.  
  46. Static variables are conceptually allocated before the program starts, and
  47. initialized the first time the block in which they are found is entered. Or, if
  48. they are outside of any function, they are initialized before execution begins.
  49. In actual implementations, typically what happens is that all statics are
  50. initialized as a big data block that forms part of your executable file. It is
  51. read (or page faulted) into memory at the correct address so that the program
  52. can access the variables.
  53.  
  54. A stack is just a form of dynamic allocation for activation records. Since most
  55. languages have nested semantics for opening and closing activation records,
  56. they are adequately modelled with a stack mechanism.
  57.  
  58.  >Should I malloc the array? How? That is to say; malloc as a
  59.  >part of the array definition, or seperately looping through
  60.  >each member of the array and mallocing it after it's been
  61.  >defined?
  62.  
  63. No you don't have to malloc it. Arrays can't be malloced: they are created in
  64. the activation record of your procedure or in static storage. Malloc can only
  65. be used for getting contiguous regions of memory referenced by a pointer
  66. variable. You can pretend that such an area is an array, but technically it is
  67. not in the strict C sense.
  68.  
  69. The statement ``struct tag arrayname[50];'' ensures that arrayname is an
  70. instantiated array with 50 elements.
  71.  
  72. If you declare this within a function without the ``static'' storage type
  73. keyword, then each recursive invocation of the function gets its own object
  74. called ``arrayname''. On the other hand, if you declare it static or put it
  75. outside of a function, only one copy will exist in the program.
  76.  
  77.  >Someone said on an irc channel about C++ that malloc is
  78.  >obsolete. "Use the new command" he said. Then logged off!!!
  79.  
  80. That person was a fool. Disregard such unenlightened advice. malloc() is a
  81. standard defined function of ANSI C. It is no better or worse than the new
  82. operator of C++. And C++ is not even standardized yet, whereas malloc() appears
  83. in an ISO standards document, which effectively gives it the upper hand.
  84. New is an operator, and not a command. Anyone who uses the word ``command'' in
  85. the place of ``operator'', ``expression'', ``function'' or ``statement'' is a
  86. dBase IV programmer in disguise and should be ignored when giving C advice.
  87.  
  88. The malloc() function is used for allocating memory that cannot be allocated
  89. statically (for example, because the amount needed cannot be stated in terms of
  90. fixed compile-time limits) but which, unlike memory allocated in nested
  91. activation records, has to survive the exit of the function in which
  92. it is created; i.e. malloc() memory exists until it is explictly removed with
  93. te free() function. You can free() memory in one function that was malloc()ed
  94. in an other, provided that you communicate the correct pointer from one to the
  95. other.
  96.  
  97. If you wanted to allocate an ``array'' of structures with malloc, you would
  98. need a pointer variable to hold that address. The pointer has to be of a
  99. suitable type for the structucture:
  100.  
  101.     struct tag *tagptr;
  102.  
  103.     tagptr = malloc(50 * sizeof(struct tag));
  104.  
  105.     if (tagptr == 0) {
  106.         printf("malloc() failed!\n");
  107.         exit(1);
  108.     }
  109.  
  110. The memory set up by malloc() has undefined contents, so you have to initialize
  111. it before use. The syntax for accessing the elements of the ``array'' is
  112. purposely the same as accessing a static or automatic array. You just say
  113. ``tagptr[3]'' to reference the fourth structure, for instance.
  114.  
  115.  >I presume there's something in C++ that relates to this, but
  116.  >I don't feel ready for OO yet. I want to learn to walk, not
  117.  >run.
  118.  
  119. More like, you have to learn to walk before you can fall on your butt. :)
  120.  
  121. There is plenty you can do in the ordinary ANSI C language that can keep you
  122. busy. Incidentally, object-oriented design and implementation is certainly
  123. possible in C, but it requires programming maturity and discipline.
  124. -- 
  125.  
  126.